//
//  SSRegisterView.swift
//  weibo
//
//  Created by 邵帅 on 16/6/23.
//  Copyright © 2016年 groupNineObject. All rights reserved.
//

import UIKit
import SnapKit

class SSRegisterView: UIView {
    
    // 登录。注册的闭包
    var loginClosure : (()->())?
    var registerClosure : (()->())?
    

    // 重写创建控件
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 添加并布局控件
    func setupUI() -> Void {
        
        // 设置一下视图的被背景颜色
        backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)
        
        // 添加
        addSubview(turnImgView)
        addSubview(maskImgView)
        addSubview(homeImgView)
        addSubview(textLable)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        // 布局
        maskImgView.snp_makeConstraints { (make) in
            
            make.center.equalTo(self.center)
        }
        
        homeImgView.snp_makeConstraints { (make) in
            
            make.center.equalTo(self.center)
        }
        
        turnImgView.snp_makeConstraints { (make) in
            
            make.center.equalTo(self.center)
        }
        
        textLable.snp_makeConstraints { (make) in
            
            make.top.equalTo(turnImgView.snp_bottom).offset(10)
            make.width.equalTo(216)
            make.centerX.equalTo(self.snp_centerX)
        }
        
        registerBtn.snp_makeConstraints { (make) in
            
            make.width.equalTo(100)
            make.height.equalTo(30)
            make.left.equalTo(textLable)
            make.top.equalTo(textLable.snp_bottom).offset(10)
        }
        
        loginBtn.snp_makeConstraints { (make) in
            
            make.width.equalTo(100)
            make.height.equalTo(30)
            make.right.equalTo(textLable)
            make.top.equalTo(registerBtn)
        }
        
    }
    
    // 核心动画
    func startAnimation() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.duration = 10
        animation.removedOnCompletion = false
        
        turnImgView.layer.addAnimation(animation, forKey: nil)
    }
    
    // 提供一个方法，可以供外界调用，修改控件属性
    func setupInfo(isHomeView : Bool, imageName : String, lblText : String) -> Void {
        
        if isHomeView == true {
            
            turnImgView.hidden = false
            startAnimation()
        }else {
            
            turnImgView.hidden = true
            homeImgView.image = UIImage(named: imageName)
            textLable.text = lblText
        }
    }
    
    
    // 懒加载控件
    // 遮盖视图
    private lazy var maskImgView : UIImageView = {
        
        let imgView = UIImageView()
        
        imgView.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return imgView
    }()
    
    
    // 房子视图
    private lazy var homeImgView : UIImageView = {
        
        let imgView = UIImageView()
        
        imgView.image = UIImage(named: "visitordiscover_feed_image_house")
        
        return imgView
    }()
    
    // 旋转视图
    private lazy var turnImgView : UIImageView = {
        
        let imgView = UIImageView()
        
        imgView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        return imgView
    }()

    // 文字
    private lazy var textLable : UILabel = {
       
        let lbl = UILabel()
        
        lbl.font = UIFont.systemFontOfSize(15)
        lbl.textColor = UIColor.darkGrayColor()
        lbl.numberOfLines = 0
        lbl.textAlignment = .Center
        lbl.text = "关注一些人，回这里看看有什么惊喜"
        
        return lbl
    }()
    
    // 登录按钮
    private lazy var loginBtn : UIButton = {
       
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: .Normal)
        btn.setTitle("登录", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        btn.addTarget(self, action: #selector(SSRegisterView.loginBtnClick), forControlEvents: .TouchUpInside)
        
        return btn
    }()
    
    // 注册按钮
    private lazy var registerBtn : UIButton = {
       
        let btn = UIButton(type: .Custom)
        
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn.setTitle("注册", forState: .Normal)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: .Normal)
        
        btn.addTarget(self, action: #selector(SSRegisterView.registerBtnClick), forControlEvents: .TouchUpInside)
        
        return btn
    }()
    
    // 登录按钮点击
    @objc private func loginBtnClick() {
        
        loginClosure?()
        
    }
    // 注册按钮点击
    @objc private func registerBtnClick() {
        
        registerClosure?()
    }
    
}
